﻿using System;
using System.Net;
using System.Text;

namespace PublicTools
{
    public class CommTools
    {
        /// <summary>
        /// 获取计算机名
        /// </summary>
        public static string LocalHostName
        {
            get
            {
                return Dns.GetHostName();
            }
        }

        /// <summary>
        /// 根据时间得到目录名yyyyMMdd
        /// </summary>
        /// <returns></returns>
        public static string GetDateDir()
        {
            return DateTime.Now.ToString("yyyyMMdd");
        }

        /// <summary>
        /// 根据时间得到文件名HHmmssff
        /// </summary>
        /// <returns></returns>
        public static string GetDateFile()
        {
            return DateTime.Now.ToString("HHmmssff");
        }

        /// <summary>
        /// 生成随机数
        /// </summary>
        /// <returns></returns>
        public static string CreateRandomNum(int length, int minValue, int maxValue)
        {
            StringBuilder sb = new StringBuilder();
            Random random = new Random();
            bool numFlag = true;

            int[] arrNum = new int[length];

            for (int i = 0; i < length; i++)
            {
                numFlag = true;
                while (numFlag)
                {
                    int j = random.Next(minValue, maxValue);
                    if (Array.IndexOf(arrNum, j) != -1)    // 生成的值在数组中存在，则继续生成
                    {
                        numFlag = true;
                    }
                    else
                    {
                        numFlag = false;  // 生成的值在数组中不存在，则放入数组中
                        arrNum[i] = j;
                    }
                }
                sb.Append(arrNum[i]);
                sb.Append(" ");
            }
            return sb.ToString();
        }
    }
}